package com.dfec.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.dfec.model.Msg;
import com.dfec.model.Praise;
import com.dfec.service.INewsService;
import com.dfec.service.IPraiseService;
import com.dfec.service.imp.NewsServiceImp;
import com.dfec.service.imp.PraiseServiceImp;
import com.dfec.utils.Consists;
import com.dfec.utils.ResponseUtil;

/**
 * Servlet implementation class SeeServlet
 */
@WebServlet(name = "praise", description = "点赞记录", urlPatterns = { "/praise" })
public class PraiseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	IPraiseService iService=new PraiseServiceImp();
    List<Object> list2=null;
    Msg msg=null;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PraiseServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		list2=new ArrayList<Object>();
		msg=new Msg();
		List<Praise> list=null;
		Map<String, Object> map=null;
		INewsService iNewsService=new NewsServiceImp();;
		try {
	        //更新浏览次数	1，获取浏览人员信息2	
			if(request.getParameter("action").equals("1")) {
				//如果是点赞addOrdel=1否则addOrdel=-1
				String addOrdel=request.getParameter("addOrdel");
				String phone=request.getParameter("phone");
				if(null==phone) {
					phone=System.currentTimeMillis()+"";
				}
				//要点赞
				if(addOrdel.equals("1")) {
					boolean hasPrai = iService.getPraiseByNewsIdAndPhone(phone,  request.getParameter("news_id"));
					if(hasPrai) {
						delPraise(request, iNewsService);
						//判断是否点赞过
//						msg.setCode(Consists.Error_Code_FAIL);
//						msg.setMsg("已经点过赞了");
//						list2.add(msg);	
					}else {
						//修改点赞次数
						iNewsService.updatePraiseBy(request.getParameter("news_id"), 1);
						//记录点赞记录
						int times = iService.updateClickPraise(phone
								, request.getParameter("news_id"),"1");
						//点赞成功，获取点赞总次数
						if(times>0) {
								msg.setCode(Consists.Error_Code_OK);
								msg.setMsg(Consists.ERROR_MSG_PRAISE_OK);
								map=new HashMap<String, Object>();
								map.put("click_praise", iNewsService.getSeeTimesOrCommentOrSeeByNewsIds("click_praise",request.getParameter("news_id"))+"");
								list2.add(msg);	
								list2.add(map);	
						   }else {
								//点赞失败
								msg.setCode(Consists.Error_Code_FAIL);
								msg.setMsg(Consists.Error_Msg_HANDLER_FAIL);
								list2.add(msg);	
							}
					}
					
				
				}else {
					
					delPraise(request, iNewsService);
				}
			
			}else if (request.getParameter("action").equals("2")) {
				list=iService.getPraisePeopleListByNewsId(request.getParameter("news_id"));
				if(list.size()>0) {
					msg.setCode(Consists.Error_Code_OK);
					msg.setMsg(Consists.Error_Msg_HANDLER_OK);
				}else {
					msg.setCode(Consists.Error_Code_FAIL);
					msg.setMsg(Consists.Error_Msg_HANDLER_FAIL);
				}
				list2.add(msg);
				list2.add(list);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			msg.setCode(Consists.Error_Code_ServerError);
			msg.setMsg(Consists.Error_Msg_ServerError);
			list2.add(msg);
		}
		
		ResponseUtil.sendScript(response, JSON.toJSONString(list2));
	}

	private void delPraise(HttpServletRequest request, INewsService iNewsService) throws SQLException {
		Map<String, Object> map;
		boolean hasPrai = iService.getPraiseByNewsIdAndPhone(request.getParameter("phone"),  request.getParameter("news_id"));
		if (hasPrai) {
			//点赞或取消点赞次数
			iNewsService.updatePraiseBy(request.getParameter("news_id"), -1);
		}
		//取消点赞
		int times = iService.updateClickPraise(request.getParameter("phone")
				, request.getParameter("news_id"),"-1");
		
		//取消点赞成功，返回点赞总次数
		if(times>0) {
			 
				msg.setCode(Consists.Error_Code_OK);
				msg.setMsg(Consists.ERROR_MSG_PRAISE_CANCEL);
				map=new HashMap<>();
				map.put("click_praise", iNewsService.getSeeTimesOrCommentOrSeeByNewsIds("click_praise",request.getParameter("news_id")));
				list2.add(msg);
				list2.add(map);	
		}else {
				//取消点赞失败
				msg.setCode(Consists.Error_Code_FAIL);
				msg.setMsg(Consists.Error_Msg_HANDLER_FAIL);
				list2.add(msg);
			}
	}

	

}
